// index.js
var app = getApp()
import Index from '../../../api/index';
import userIndex from '../../../api/user';
import shopCar from '../../../api/shopCar';
Page({
  data: {
    navBarHeight:20,
    navHeight:40,
    tabbar: {},
    carNum:0,
    isPhone:app.globalData.isPhone,
    modelPhone:false,
    isIphoneX:app.globalData.isIphoneX,
    indicatorDots: true,
    vertical: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    adList: [],//公告列表
    bannerList: [],//banner列表
    popList: [],//首页弹窗列表
    isPop:false,

    label:1,
    pageNo:1,
    pageSize:20,
    sort:'',
    title:'',
    typeId:'',

    productList:[],
    isMore:false,

    active:0,//满减活动开关{1:开启,0:关闭}
    levelList:[],//满减级别
    totalAmount:0,//累计金额
    maxAmount:0,//满减最大值

    sekKillActive:0,
    sekKillList:[],

    orderBuyInf:[]
  },
  getBanner(){
    Index.apiBannerInfor().then(res=>{
      var isPop=false
      if(res.data.popList&&res.data.popList.length>0&&app.globalData.homeNumber==1){
        isPop=true
        app.globalData.homeNumber=2
      }
        this.setData({
          adList: res.data.adList,//公告列表
          bannerList: res.data.bannerList,//公告列表
          popList:res.data.popList,//公告列表
          isPop:isPop
        })        
    })
  },
  closePop(){
    app.globalData.homeNumber=2
    this.setData({
      isPop:false      
    })
  },
  getlabel(e){
    this.setData({      
      label:e.currentTarget.dataset.label,
      pageNo:1,
    })
    this.getProductList()
  },
  //获取产品
  getProductList(){
    var opt={
      label:this.data.label,
      pageNo:this.data.pageNo,
      pageSize:this.data.pageSize,
      sort:this.data.sort,
      title:this.data.title,
      typeId:this.data.typeId,
    }
    Index.apiProductList(opt).then(res=>{
      var list=this.data.productList||[]
        var isMore=true
        if(opt.pageNo==1){
          list=res.data.list
        }else{
          list=list.concat(res.data.list)
        }
        if(res.data.list.length<opt.pageSize){
          isMore=false
        }
        this.setData({
          productList:list,
          isMore:isMore
        })
    })
  },
  //满减活动进度
  getFullGiftProgress(){
    Index.apiFullGiftProgress().then(res=>{
      var maxAmount=0
      if(res.data.levelList&&res.data.levelList.length>0){
        maxAmount=Math.max(...res.data.levelList);
      }
      this.setData({
        active:res.data.active,
        levelList: res.data.levelList,
        totalAmount: res.data.totalAmount,
        maxAmount:maxAmount
      })
    })
  },
  //限时秒杀产品
  getMarketingactivitySekKillRroducts(){
    Index.apiMarketingactivityWelcomeSekKill().then(res=>{
      var sekKillList=[]
      if(res.data.list&&res.data.list.length>0){
        sekKillList=res.data.list.map(item=>{
          // item.img=item.img.split(",")[0]
          return item
        })
      }
      
        this.setData({
          sekKillActive:res.data.active,
          sekKillList:sekKillList
        })
    })
  },
  //满赠详情
  fullGift(){
    wx.navigateTo({
      url: '/pages/home/fullGift/fullGift',
    })
  },
  goShopDetails(e){
    var row=e.currentTarget.dataset.row
    wx.navigateTo({
      url: `/pages/home/productDetails/productDetails?id=${row.id}`,
    })
  },
  getShopCarNum(){
    shopCar.apishopCarNum().then(res=>{
      this.setData({
        carNum:res.data?res.data.num:0,
      })
    })
  },
  onLoad(){  
    // wx.showShareMenu({  
    //   menu: ['shareAppMessage', 'shareTimeline'] // 若只需“发送给朋友”，则写 ['shareAppMessage']  
    // });  
     // 页面加载
     var that = this
     if(app.globalData.isIphoneX){
       this.setData({
         isIphoneX:app.globalData.isIphoneX
       })
     }else{
       setTimeout(()=>{
         that.setData({
           isIphoneX:app.globalData.isIphoneX
         })
       },100)
     }
     this.getBanner()
     this.getProductList()
    
     
     this.getBroadcast()
     app.editTabBar()
     
  },
  
  onShow(){
    var that=this
    
    setTimeout(()=>{
      that.setData({
        isPhone:app.globalData.isPhone,
      })
      if(app.globalData.isPhone&&wx.getStorageSync('token')){
        that.getShopCarNum()
      }
      if(wx.getStorageSync('token')){
        that.getFullGiftProgress()
        that.getMarketingactivitySekKillRroducts()
       }
    },1000)
    
  },
  getLoginBtn(){
    this.setData({
      modelPhone:true,
    })
  },
  closeLoginBtn(){
    this.setData({
      modelPhone:false,
    })
  },
  getPhoneNumber(e){
    var that=this
    var code=e.detail.code
    if(code){
      userIndex.apigetPhone({code:code}).then(res=>{
        app.globalData.isPhone=res.data.phone?true:false
        this.setData({
          isPhone:res.data.phone?true:false,
          modelPhone:false
        })
        this.getFullGiftProgress()
        this.getMarketingactivitySekKillRroducts()
      })
    }
  },
  //购买广播
  getBroadcast(){
    Index.apiOrderBuyInfo().then(res=>{
      this.setData({
        orderBuyInf:res.data
      })
    })
  },
  tapBanner(e){
    var row=e.currentTarget.dataset.row
    if(row.linkType==1){
      wx.navigateTo({
        url: `/pages/home/productDetails/productDetails?id=${row.productLinkUrl}`,
      })
    }else{
      if(row.otherLinkUrl.includes('/pages')){
        wx.navigateTo({
          url:row.otherLinkUrl
        })
      }else{
        wx.navigateTo({
          url: `/pages/home/webView/webView?url=${encodeURIComponent(row.otherLinkUrl)}`,
        })
      }
      
    }
  },
  getPanicBuy(){
    wx.navigateTo({
      url: `/pages/home/panicBuy/panicBuy`,
    })
  },
  getseaarch(){
    wx.navigateTo({
      url: '/pages/classify/search/search',
    })
  },
    /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.isMore){
      this.setData({
        pageNo:this.data.pageNo+1
      })
      this.getProductList()
    }
  },
  onShareAppMessage: function () {
    var userId=wx.getStorageSync('userInfo')?wx.getStorageSync('userInfo').userId:''
    return {
      title: '火食集', // 分享标题
      path: `/pages/home/index/index?referrerId=${userId}`, // 分享路径，必须是已经存在的页面路径
      // imageUrl: 'https://example.com/share.jpg', // 分享图标
      success: function (res) {
        // 转发成功
        if (res.errMsg === 'shareAppMessage:ok') {
          console.log('分享成功');
        }
      },
      fail: function (res) {
        // 转发失败
        console.log('分享失败', res);
      }
    }
  },
  onShareTimeline() {  
    var userId=wx.getStorageSync('userInfo')?wx.getStorageSync('userInfo').userId:''
    return {
      title: '火食集', // 分享标题
      path: `/pages/home/index/index?referrerId=${userId}`, // 分享路径，必须是已经存在的页面路径
      // imageUrl: 'https://example.com/share.jpg', // 分享图标
      success: function (res) {
        // 转发成功
        if (res.errMsg === 'shareAppMessage:ok') {
          console.log('分享成功');
        }
      },
      fail: function (res) {
        // 转发失败
        console.log('分享失败', res);
      }
    }  
  }  
})
